﻿using System.Buffers;
using System.Collections.Generic;
using System.Text.Json.Serialization;

using Microsoft.Extensions.DependencyInjection;

using Xc.DataAccess.Core.Extensions;
using Xc.DataAccess.Protocoals.WaterMeters.Extensions;

namespace Xc.DataAccess.Protocoals.WaterMeters.Models.Responds
{
    /// <summary>
    /// 读取表计数据
    /// </summary>
    public class GetMeterDataRespond : MeterFrameRxDataSegmentBase
    {
        /// <summary>
        /// /读取表计数据
        /// </summary>
        /// <param name="dataArea"></param>
        public GetMeterDataRespond(MeterFrameRx dataArea) : base(dataArea)
        {
        }

        public GetMeterDataRespond()
        {
        }

        protected override bool SetData(ref SequenceReader<byte> sequenceReader)
        {
            if (!sequenceReader.TryRead(4,out ReadOnlySequence<byte> cumulativeFlow))
            {
                return false;
            }
            CumulativeFlow = cumulativeFlow.ToArray().ToUInt32DecimalLittleEndian(100.000M);
            if (!sequenceReader.TryRead(out var unit))
            {
                return false; 
            }
            CumulativeFlowUnit =(MeterUnit)unit;
            if (!sequenceReader.TryRead(4, out ReadOnlySequence<byte> remainFlow))
            {
                return false;
            }
            RemainFlow = remainFlow.ToArray().ToUInt32DecimalLittleEndian(100.000M);
            if (!sequenceReader.TryRead(out var unitminer))
            {
                return false;
            }
            if (unitminer==0xC2)
            {
                RemainFlow = RemainFlow * -1;
            }
            RemainFlowUnit = (MeterUnit)unitminer;
            if (!sequenceReader.TryRead(6, out ReadOnlySequence<byte> dateTime))
            {
                return false;
            }
            this.DateTime = dateTime.ToArray().ToDateTime();
            if (!sequenceReader.TryRead(2, out ReadOnlySequence<byte> status))
            {
                return false;
            }
            this.MeterStatus = new MeterStatus(status.ToArray());
            return true;
        }

        internal override DataSegment ValidateDataSegment => DataSegment.GetMeterData;

        internal override IEnumerable<FrameCommand> ValidateCmd 
        {
            get
            {
                yield return FrameCommand.CMD01RepOK;
                yield return FrameCommand.CMD01RepNG;
            }
        }

        /// <summary>
        /// 累积流量
        /// </summary>
        public decimal CumulativeFlow { get;private set; }
        /// <summary>
        /// 单位
        /// </summary>
        public MeterUnit CumulativeFlowUnit { get;private set; }
        /// <summary>
        /// 剩余流量
        /// </summary>
        public decimal RemainFlow { get;private set; }
        /// <summary>
        /// 单位
        /// </summary>
        public MeterUnit RemainFlowUnit { get; private set; }
        /// <summary>
        /// 实时时间
        /// </summary>
        public string DateTime { get;private set; }
        /// <summary>
        /// 表具状态
        /// </summary>
        
        public MeterStatus MeterStatus { get;private set; }
    }
    /// <summary>
    /// 计量单位
    /// </summary>
    public enum MeterUnit
    {
        /// <summary>
        /// 吨
        /// </summary>
        Ton=0x2C|0xC2,
    }
}
